﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using Extjs_ReGSharp.InputParsing;
using Extjs_ReGSharp.Sorting;
using Extjs_ReGSharp.Interfaces;
using System.Collections;

namespace Extjs_ReGSharp
{
	public class UserRequestExecuter<T> where T : IGridDisplayItem
	{
		#region Data Memebers

		private GridSource<T> m_GridSource;

		List<ISortingBehaviourFactory<T>> m_CustomRegisteredSortingBehaviours;

		#endregion

		#region Constructor

		public UserRequestExecuter(GridSource<T> gridSource)
		{
			m_GridSource = gridSource;

			m_CustomRegisteredSortingBehaviours = new List<ISortingBehaviourFactory<T>>();
		}

		#endregion

		#region Properties

		public List<T> SourceItems
		{
			get
			{
				return m_GridSource.SourceItems;
			}
		}

		#endregion
		
		#region Public Methods

		public GridResult<T> GetItemsByRequestParameters(NameValueCollection inputParameters)
		{
			InputParser<T> inputParser = new InputParser<T>(inputParameters);

			Filter<T> filter = inputParser.ExtractFilter();

			Sorter<T> sorter = inputParser.ExtractSorter(m_CustomRegisteredSortingBehaviours);

			PageData<T> pageData = inputParser.ExtractPageData();

			GridResult < T > result = m_GridSource.GetItems(filter, sorter, pageData);

			return result;
		}

		public void RegisterSortBehaviour(ISortingBehaviourFactory<T> sortingBehaviourFactory)
		{
			m_CustomRegisteredSortingBehaviours.Insert(0, sortingBehaviourFactory);
		}

		#endregion
	}
}
